import 'dart:io';
import 'package:baselib/utils/drawable_base_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/**
 * 图片加载管理类
 */
class ImageUtil{

  static bool isURL(String str) {
    if((str??'').isNotEmpty&&(str.startsWith('http'))){
      return true;
    }
    return false;
  }
  //加载图片
  static Widget loadImage(String imgPath,
      {Key? key,BorderRadius? radius,String? placeholder,
        double? width, double? height, BoxFit? fit,String? package}){
    if((imgPath??'').startsWith('http')){
     return loadNetworkImage(imgPath,key:key,width: width,height: height,
         radius: radius??BorderRadius.zero,fit: fit??BoxFit.cover,placeholder: placeholder??"none");
    }else{
      return loadAssetImage(imgPath,key: key,width: width,height: height,
          radius: radius??BorderRadius.zero,fit: fit??BoxFit.cover,package: package);
    }
  }
  /// 加载本地资源图片
  static Widget loadAssetImage(String imgPath, {Key? key,double? width, double? height,
      BoxFit? fit,BorderRadius? radius,Color? color,String? package,bool? isOrigin}){
    return ClipRRect(
      key: key,
      borderRadius: radius??BorderRadius.zero,
      child:
      imgPath.endsWith('.svg')?
      ((isOrigin??false)?
      SizedBox(
        width: width,
        height: height,
        child: SvgPicture.asset(
          imgPath,
          semanticsLabel: '',
          width: width,
          height: height,
          fit: fit??BoxFit.cover,
          package: package,
        ),
      ):
      SizedBox(
        width: width,
        height: height,
        child: SvgPicture.asset(
          imgPath,
          colorFilter: ColorFilter.mode(color??Colors.black54, BlendMode.srcIn),
          semanticsLabel: '',
          width: width,
          height: height,
          fit: fit??BoxFit.cover,
          package: package,
        ),
      )):
      ImageUtil.loadAssetImageOrigin(
          imgPath,
          width: width,
          height: height,
          fit: fit??BoxFit.cover,
          color: color,
          package: package
      ),
    );
  }
  static Widget loadImageFile(File file,
      {double? width, double? height, BoxFit? fit,double? roundRadius,
        String? placeholder,double? placeWidth,Color? placeholderColor,String? package
      }){
    return ClipRRect(
      borderRadius: BorderRadius.circular(roundRadius??0),
      child: ImageUtil.loadImageFileOrigin(
          file,
          width: width??0,
          height: height??0,
          fit: fit,
          placeholder: placeholder,
          placeWidth: placeWidth,
          placeholderColor: placeholderColor,
          package: package
      ),
    );
  }
  static Widget loadImageFileOrigin(File file,
      {double? width, double? height, BoxFit? fit,double? roundRadius,String? placeholder,double? placeWidth,Color? placeholderColor,String? package}){
    Image image = Image.file(
      file, height: height, width: width, fit: fit??BoxFit.cover,
      errorBuilder: (context, url, error){
        return (placeholder=='none'||(placeholder??'').isEmpty)?
        Container(child:
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ImageUtil.loadAssetImage(
              drawable_base_icon_empty,
              isOrigin: true,
              width: placeWidth??px_base_150.w,
              fit: BoxFit.fitWidth,
              color: placeholderColor,
              package: package,
              radius: BorderRadius.circular(roundRadius??0),
            )
          ],
        ),
        ):
        loadAssetImage(
          placeholder??'none',color: placeholderColor, height: height, width: width, fit: fit??BoxFit.cover,package: package,
          radius: BorderRadius.circular(roundRadius??0),
        );
      },
    );
    final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
    stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
    },onError: (dynamic exception, StackTrace? stackTrace) {
    } ));
    return image;
  }
  static Widget loadAssetImageOrigin(String imgPath,
      {double? width, double? height, BoxFit? fit,Color? color,String? package}){
    try{
      Image image = Image.asset(

        imgPath??'', height: height, width: width, fit: fit,color: color,package: package,
      );
      final ImageStream stream = image.image.resolve(ImageConfiguration.empty);
      stream.addListener(ImageStreamListener((ImageInfo image, bool synchronousCall){
      },onError: (dynamic exception, StackTrace? stackTrace) {
      } ));
      return image;
    }catch(e){
      return SizedBox(width: 0,);
    }
  }

  /// 加载网络图片
  static Widget loadNetworkImage(String imgPath,
      {Key? key,BorderRadius? radius,
        Color? color,
        String? placeholder, double? placeWidth,
        double? width, double? height,
        BoxFit? fit,String? package,Color? placeholderColor}){

    if(placeholder==null)placeholder = 'none';
    return Container(
      width: width,
      height: height,
      child: ClipRRect(
        borderRadius: radius??BorderRadius.zero,
        child: CachedNetworkImage(
          key: key,
          imageUrl: imgPath??'',
          color: color,
          placeholder: (context, url) => (placeholder=='none')?CupertinoActivityIndicator()
              :loadAssetImage(placeholder??'none', height: height, width: width, fit: fit??BoxFit.cover,color: placeholderColor,package: package),
          // errorWidget: (context, url, error) => (placeholder=='none')?Container(child: Icon(Icons.error),):
          errorWidget: (context, url, error) => (placeholder=='none')?
          Container(child:
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  ImageUtil.loadAssetImage(
                      drawable_base_icon_empty,
                      isOrigin: true,
                      width: placeWidth??px_base_150.w,
                      fit: BoxFit.fitWidth,
                      color: placeholderColor
                  )
                ],
              ),
          ):
          loadAssetImage(
              placeholder??'none',color: placeholderColor, height: height, width: width, fit: fit??BoxFit.cover,package: package
          ),
          width: width,
          height: height,
          fit: fit??BoxFit.cover,
        )
      ),
    );

  }

  /// 加载网络图片
  static Widget FadeInImageAssetNetwork(String imgPath,
      {String? placeholder, double? width, double? height, BoxFit? fit}){
    print(imgPath);
    return FadeInImage.assetNetwork(
      image: imgPath == null ? "" : imgPath,
      placeholder:placeholder??"none",
      width: width,
      height: height,
      fit: fit??BoxFit.cover,
    );
  }

}
